package com.plane;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;

public class Npc extends Sprite {
    private static Image[] images;
    private int moveType;
    private int moveCount;
    private double dLine = 0;
    private int count;
    private Hero hero;
    private int distanceX = 0;
    private int distanceY = 0;

    static {
        try {
            images = new Image[]{
                    ImageIO.read(Npc.class.getResourceAsStream("npc0_0.png")),
                    ImageIO.read(Npc.class.getResourceAsStream("npc1_0.png")),
                    ImageIO.read(Npc.class.getResourceAsStream("npc2_0.png")),
                    ImageIO.read(Npc.class.getResourceAsStream("e5.png"))
            };
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Npc(int[] data, Hero hero) {
        //this.image = images[data[7]];
        this.hero = hero;
        this.x = data[0];
        this.y = data[1];
        this.width = data[2];
        this.height = data[3];
        this.xStep = data[4];
        this.yStep = data[5];
        this.hp = data[6];
        this.image = images[data[7]];
        this.moveType = data[8];
        this.moveCount = data[9];
    }

    public void move(ArrayList npcs) {
        count++;
        if (moveType == 0) {
            this.x += xStep;
            this.y += yStep;
        } else if (moveType == 1) {
            this.x += xStep;
            if (count < moveCount) {
                this.y += yStep;
            } else {
                this.y -= yStep;
            }
        } else if (moveType == 2) {
            if (count < moveCount) {
                this.x += xStep;
                this.y += yStep;
            }
        } else {
            if (count < moveCount) {
                distanceX = hero.x - x;
                distanceY = hero.y - y;
                dLine = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
            }
            this.x += (distanceX / dLine) * xStep;
            this.y += (distanceY / dLine) * yStep;
        }
        if (x <-width || x>Map.MAP_WIDTH || y<-height){
            npcs.remove(this);
        }

    }

    public void paint(Graphics gTmp, GamePanel gp) {
        gTmp.drawImage(image, x, y, gp);
    }
}
